<template>
	<view class="pageBox">
		<view class="main">
			<view class="mainItem flex-between" @click="goPage('/setting/infomation')">
				<view class="title">
					个人资料
				</view>
				<image class="arrowRight" src="http://www.bobei.shop/static/home/arrowRight.png" mode=""></image>
			</view>
							<!-- #ifdef APP-PLUS	 -->
			<view class="mainItem flex-between" @click="bindWx"  v-show="wxOpenid == ''">
				<view class="title">
					绑定微信  {{wxOpenid}}
				</view>
				<image class="arrowRight" src="http://www.bobei.shop/static/home/arrowRight.png" mode=""></image>
			</view>
						<!-- #endif -->

			<view class="mainItem flex-between" @click="goPage('/pages/my/account')">
				<view class="title">
					账号与安全
				</view>
				<image class="arrowRight" src="http://www.bobei.shop/static/home/arrowRight.png" mode=""></image>
			</view>


			<!-- #ifdef APP-PLUS	 -->
			<view class="mainItem flex-between" @click="onClearStorage">
				<view class="title">
					清理缓存
				</view>
				<view class="value">{{fileSizeString}}</view>
				<image class="arrowRight" src="http://www.bobei.shop/static/home/arrowRight.png" mode=""></image>
			</view>
			<!-- #endif -->
			<!-- #ifdef APP-PLUS	 -->
			<view class="mainItem flex-between" @click="getVersion">
				<view class="title">
					当前版本
				</view>
				<image class="arrowRight" src="http://www.bobei.shop/static/home/arrowRight.png" mode=""></image>
			</view>
			<!-- #endif -->
			
			<view class="mainItem flex-between" @click="goPage('/setting/about?name=about_us&title=关于我们')">
				<view class="title">
					关于我们
				</view>
				<image class="arrowRight" src="http://www.bobei.shop/static/home/arrowRight.png" mode=""></image>
			</view>

			<view class="mainItem flex-between" @click="goPage('/setting/about?name=privacy_policy&title=隐私协议')">
				<view class="title">
					隐私协议
				</view>
				<image class="arrowRight" src="http://www.bobei.shop/static/home/arrowRight.png" mode=""></image>
			</view>
		</view>

		<button class="signOut" :plain="true" @click="loginOut">退出登录</button>

		<determined-popover :showTips="determinedShow" @tipsOpen="determineTipsOpen" @tipsClose="determineTipsClose"
			@onDeterClose="onDeterClose" @onPrefect="determineOnPrefect" :deterTitle="deterTitle"
			:deterContent="deterContent"></determined-popover>

		<determined-popover :showTips="determinedShow2" @tipsOpen="determineTipsOpen2" @tipsClose="determineTipsClose2"
			@onDeterClose="onDeterClose2" @onPrefect="determineOnPrefect2" :deterTitle="deterTitle2"
			:deterContent="deterContent2"></determined-popover>
	</view>
</template>

<script>
	import determinedPopover from "../components/determined-popover/determined-popover.vue";
	export default {
		components: {
			"determined-popover": determinedPopover,
		},
		data() {
			return {
				determinedShow: false,
				deterTitle: "",
				deterContent: "",

				determinedShow2: false,
				deterTitle2: "",
				deterContent2: "",

				fileSizeString: "",
				wxOpenid:''
			}
		},
		
		methods: {
			onClearStorage() {
				this.deterTitle = "提示";
				this.deterContent = "确定要清除缓存吗？";
				this.determinedShow = true;
			},
			determineTipsOpen(param) {
				this.determinedShow = param;
			},
			determineTipsClose(param) {
				this.determinedShow = param;
			},
			onDeterClose() {
				this.determinedShow = false;
			},
			determineOnPrefect() {
				this.determinedShow = false;
				this.clearStorage();
			},

			// #ifdef  APP-PLUS
			getVersion() {
				let version = plus.runtime.version;
				this.deterTitle2 = "提示";
				this.deterContent2 = "当前版本号" + version;
				this.determinedShow2 = true;
			},
			// #endif
			determineTipsOpen2(param) {
				this.determinedShow2 = param;
			},
			determineTipsClose2(param) {
				this.determinedShow2 = param;
			},
			onDeterClose2() {
				this.determinedShow2 = false;
			},
			determineOnPrefect2() {
				this.determinedShow2 = false;
			},
			bindWx(){
				let that = this;
				uni.login({
					"provider": "weixin",
					"onlyAuthorize": true, // 微信登录仅请求授权认证
					success: function(event) {
						// console.log("微信登录:", event)
						const {
							code
						} = event
						//客户端成功获取授权临时票据（code）,向业务服务器发起登录请求。
						that.$post({
							module: 'User',
							interface: 100001,
							data: {
								code: event.code,
							},
						}).then(res => {
							// console.log("微信APP登录:", res)
						uni.showToast({
							icon: "none",
							title: res.message,
							duration: 1000
						})
							uni.navigateBack()
							
						}).catch((error) => {
							// console.log("微信登录传code报错",error)
						})
					},
					fail: function(err) {
						// 登录授权失败  
						// err.code是错误码
						// console.log("登录授权失败err:", err);
					}
				})
				
			},

			goPage(url) {
				uni.navigateTo({
					url: url
				})
			},
			loginOut() {
				this.$post({
					module: 'Account',
					interface: 2000
				}).then(res => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 1500,
					})
					uni.removeStorageSync('token')
				})
			},
			// 计算缓存
			getStorageSize() {
				let that = this;
				plus.cache.calculate(function(size) {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						that.fileSizeString = "0B";
						// console.log(that.fileSizeString);
					} else if (sizeCache < 1024) {
						that.fileSizeString = sizeCache + "B";
						// console.log(that.fileSizeString);
					} else if (sizeCache < 1048576) {
						that.fileSizeString = (sizeCache / 1024).toFixed(2) + "K";
						// console.log(that.fileSizeString);
					} else if (sizeCache < 1073741824) {
						that.fileSizeString = (sizeCache / 1048576).toFixed(2) + "M";
						// console.log(that.fileSizeString);
					} else {
						that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "G";
						// console.log(that.fileSizeString);
					}
				});
			},

			// 清理缓存
			clearStorage() {
				let that = this;
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录
									uni.showToast({
										title: '缓存清理完成',
										icon: "none",
										duration: 2000,
									});
									that.getStorageSize(); // 重新计算缓存
								}, function(e) {
									// console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							// console.log('文件路径读取失败')
						});
					}
				} else { // ios
					plus.cache.clear(function() {
						uni.showToast({
							title: '缓存清理完成',
							icon: "none",
							duration: 2000,
						});
						that.getStorageSize();
					});
				}
			},


		},
		onLoad(e) {
			// #ifdef APP
			this.getStorageSize()
			this.wxOpenid = e.wxOpenid
			console.log(e);
			// console.log(this.fileSizeString)
			// #endif
		}
	}
</script>

<style scoped lang="scss">
	.pageBox {
		width: 100%;
		min-height: calc(100vh - var(--window-top));
		padding-top: 32rpx;
	}

	.main {
		background: linear-gradient(180deg, #FFF5F4 0%, #FFFFFF 14%);
		border-radius: 24rpx 24rpx 0rpx 0rpx;
		width: 100%;
		height: 1156rpx;
		overflow: hidden;

		.mainItem {
			width: 100%;
			padding: 32rpx;

			.title {
				flex: 1;
				font-size: 32rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 600;
				color: #000000;
				line-height: 40rpx;
			}

			.value {
				font-size: 26rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
				line-height: 38rpx;
				margin-right: 10rpx;
			}

			.arrowRight {
				width: 28rpx;
				height: 28rpx;
			}
		}
	}

	.signOut {
		padding: 0;
		margin: 0 auto 0;
		font-size: 36rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
		width: 682rpx;
		height: 90rpx;
		background: linear-gradient(256deg, #F2615F 0%, #FEA20E 100%);
		border-radius: 52rpx;
		border: none;
	}
</style>
